package com.rural.bus.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import cn.jpush.android.api.JPushInterface;

import com.rural.bus.BaseActivity;
import com.rural.bus.MyApplication;
import com.rural.bus.config.MyPreferenceManager;
import com.rural.bus.config.PreferenceKey;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;

public class User {
	public String userId;
	public String name;
	public String phone;
	public Railway currentRailway;
	
	/***
	 * loginin
	 * @param context
	 * @param phonenum
	 * @param psw
	 * @param callback
	 */
	public void login(final Context context,String phonenum,String psw,String regionId,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("phone", phonenum);
		http.addBodyParm("password", psw);
		http.addBodyParm("regionId", regionId);

		http.send(Url.login_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context,object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}
	
	/***
	 * parse json receive from internet
	 * @param context
	 * @param object
	 */
	private void parse(Context context,JSONObject object){
		if(object==null){
			return;
		}
		try {
			JSONObject o=object.getJSONObject("baseInfo");
			this.name=o.getString("name");
			this.phone=o.getString("phone");
			this.userId=o.getString("userId");
			MyApplication app=(MyApplication) context.getApplicationContext();
			app.setCurrentUser(this);
			User.saveUserToPref(context, this);
			
			MyPreferenceManager.instance().putSharedInfoOfString(PreferenceKey.USER_ID, o.getString("userId"));
			MyPreferenceManager.instance().putSharedInfoOfString(PreferenceKey.USER_NAME, o.getString("name"));
			MyPreferenceManager.instance().putSharedInfoOfString(PreferenceKey.USER_PHONE, o.getString("phone"));

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/***
	 * save user to sharedpreference
	 * @param context
	 * @param user
	 */
	public static void saveUserToPref(Context context,User user){
//		SuUtil su=new SuUtil(context, "_user");
		MyPreferenceManager manager=MyPreferenceManager.instance();
		manager.putSharedInfoOfString("userId", user.userId);
		manager.putSharedInfoOfString("name", user.name);
		manager.putSharedInfoOfString("phone", user.phone);
	}
	
	/***
	 * logout
	 * @param context
	 */
	public static void logout(Context context){
		MyApplication app=(MyApplication) context.getApplicationContext();
		app.setCurrentUser(null);
		BaseActivity activity=(BaseActivity) context;
		activity.mCurrentUser=null;
		
//		SuUtil su=new SuUtil(context, "_user");
//		su.clear();
		MyPreferenceManager manager=MyPreferenceManager.instance();
		manager.clearPreferences();
	}
	
	/***
	 * get user from sharedpreference
	 * @param context
	 * @return
	 */
	public static User getUserFromPref(Context context){
		User user=new User();
//		SuUtil su=new SuUtil(context, "_user");
		MyPreferenceManager manager=MyPreferenceManager.instance();
		user.userId=manager.getSharedInfoByKey("userId", "");
		user.name=manager.getSharedInfoByKey("name", "");
		user.phone=manager.getSharedInfoByKey("phone", "");
		if(TextUtils.isEmpty(user.userId)){
			return null;
		}
        return user;
	}
	/***
	 * 用户注册
	 */
	public void register(final Context context,String name,String password,String phone,
			String regionId,String remarks,int sex,final OnNetCallback callback){
		
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("name", name);
		http.addBodyParm("password", password);
		http.addBodyParm("phone", phone);
		http.addBodyParm("regionId", regionId);
		http.addBodyParm("remarks", remarks);
		
		http.addBodyParm("sex", ""+sex);
		http.send(Url.register_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context,object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});

	}
	
	/***
	 * 获取历史订单
	 * @param start
	 * @param end
	 * @param status
	 * @param callback
	 */
	public void getOrderList(int start,int end,int status,OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("udid", ""+userId);
		http.addBodyParm("type", ""+0);
		http.addBodyParm("start", ""+start);
		http.addBodyParm("end", ""+end);
//		http.addBodyParm("status", ""+status);
		
		http.send(Url.orderlist_url, callback);

	}
	
	/***
	 * 获取服务中的订单
	 */
	public void getInServiceOrderList(int start,int end,int status,OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("udid", ""+userId);
		http.addBodyParm("type", ""+0);
		http.addBodyParm("start", ""+start);
		http.addBodyParm("end", ""+end);
		
		http.send(Url.inservice_orderlist_url, callback);
	}
	
	/***
	 * 更新用户的信息
	 * @param callback
	 */
	public void update(Context context,final OnNetCallback callback){
		
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("userId", userId);
		http.addBodyParm("regionId", JPushInterface.getRegistrationID(context));
		http.send(Url.update_url, callback);
		
	}

}
